package com.atgm.mall.gmmailmember.service.impl;

import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.MemberException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmailmember.dao.MemberThumbsCommentDao;
import com.atgm.mall.gmmailmember.entity.MemberThumbsCommentEntity;
import com.atgm.mall.gmmailmember.feign.ClubFeignService;
import com.atgm.mall.gmmailmember.service.MemberThumbsCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Member;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class MemberThumbsCommentServiceImpl extends ServiceImpl<MemberThumbsCommentDao, MemberThumbsCommentEntity> implements MemberThumbsCommentService {

    @Autowired
    private ClubFeignService clubFeignService;

    @Override
    public Integer updateCommentThumbs(Long commentId, Long memberId) {
        //检查该用户是否点在该评论
        QueryWrapper<MemberThumbsCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        queryWrapper.eq("comment_id", commentId);
        queryWrapper.eq("member_id", memberId);
        MemberThumbsCommentEntity selectOne = baseMapper.selectOne(queryWrapper);
        int flag = 0;  //点赞数量 0：-1 1：-1
        if (selectOne == null) {
            MemberThumbsCommentEntity memberCommentThumbsEntity = new MemberThumbsCommentEntity();
            memberCommentThumbsEntity.setCommentId(commentId);
            memberCommentThumbsEntity.setMemberId(memberId);
            memberCommentThumbsEntity.setDeleted(0);
            memberCommentThumbsEntity.setCreateTime(new Date());
            int count = baseMapper.insert(memberCommentThumbsEntity);
            if (count != 1) {
                throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
            }
            flag = 1;
        }else {
            int count = baseMapper.deleteById(selectOne.getId());
            if (count != 1) {
                throw new MemberException("取消点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
            }
        }

        R r = clubFeignService.updateCommentThumbsCount(commentId, flag);
        if (0 != r.getCode()) {
            throw new MemberException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }
        return flag;
    }

    @Override
    public List<Long> haveCommentThumbsId(Long memberId) {
        QueryWrapper<MemberThumbsCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("comment_id");
        queryWrapper.eq("member_id", memberId);
        List<MemberThumbsCommentEntity> list = baseMapper.selectList(queryWrapper);

        List<Long> outList = new ArrayList<Long>();
        if (GmMallUtils.isNotEmpty(list)) {
            outList = list.stream().map(MemberThumbsCommentEntity::getCommentId).collect(Collectors.toList());
        }
        return outList;
    }
}
